package com.hbm.usercenter.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hbm.usercenter.R;
import com.hbm.usercenter.utils.LogUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by BS on 2017/6/8.
 */

public class GroupInfo extends LinearLayout {
    @Bind(R.id.tv)
    TextView mTv;
    @Bind(R.id.tv_name)
    TextView mTvName;
    @Bind(R.id.h_line)
    View mHLine;

    public GroupInfo(Context context) {
        super(context);
        initView(context);
    }

    public GroupInfo(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initTypedArray(context,attrs);
    }

    public GroupInfo(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initTypedArray(context,attrs);
    }

    private void initView(Context context) {
        if (!isInEditMode()) {
            View view = LayoutInflater.from(context).inflate(R.layout.group_info, this, true);
            ButterKnife.bind(this, view);
        }
    }

    private void initTypedArray(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GroupInfo);

        if (null != typedArray) {

            String gourp_tv = typedArray.getString(R.styleable.GroupInfo_group_tv);
            String group_name = typedArray.getString(R.styleable.GroupInfo_group_name);
            //是否有分割线
            boolean isLine = typedArray.getBoolean(R.styleable.GroupInfo_group_info_is_line, true);
            //是否可编辑
            boolean isEt = typedArray.getBoolean(R.styleable.GroupInfo_group_info_is_et, false);
            if (!isInEditMode()) {
                if (!TextUtils.isEmpty(gourp_tv)) {
                    mTv.setText(gourp_tv);
                }
                if (!TextUtils.isEmpty(group_name)) {
                    mTvName.setText(group_name);
                }
                if (mHLine != null) {
                    if (isLine) {
                        mHLine.setVisibility(View.VISIBLE);
                        LogUtils.d("hahah true");
                    } else {
                        mHLine.setVisibility(View.INVISIBLE);
                        LogUtils.d("hahah false");
                    }
                }
                if (!isEt) {
                    mTvName.setFocusable(false);
                    mTvName.setFocusableInTouchMode(false);
                }
            }
        }
        typedArray.recycle();
    }

    public void setText(String text) {
        mTvName.setText(text);
    }
    public String getText() {
        return  mTvName.getText().toString().trim();
    }
}
